package com.shawn.datastructure.stack;

import java.util.Stack;

public class ValidParentheses {

	public boolean isValid(String s) {
		if (s == null && s.isEmpty()) {
			return true;
		}
		char[] arr = s.toCharArray();
		Stack<Character> stack = new Stack<>();

		for (int i = 0; i < arr.length; i++) {
			char c = arr[i];
			switch (c) {
				case '(':
				case '{':
				case '[':
				case '<':
					stack.push(c);
					break;
				case ')':
					if (!stack.isEmpty() && stack.peek() == '(') {
						stack.pop();
						break;
					} else {
						return false;
					}
				case '}':
					if (!stack.isEmpty() && stack.peek() == '{') {
						stack.pop();
						break;
					} else {
						return false;
					}
				case ']':
					if (!stack.isEmpty() && stack.peek() == '[') {
						stack.pop();
						break;
					} else {
						return false;
					}
				case '>':
					if (!stack.isEmpty() && stack.peek() == '<') {
						stack.pop();
						break;
					} else {
						return false;
					}
			}
		}
		return stack.isEmpty();
	}

	/*
	        int length;

        do {
            length = s.length();
            s = s.replace("()", "").replace("{}", "").replace("[]", "");
        } while(length != s.length());

        return s.length() == 0;
	 */

}
